﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel;
using System.ServiceModel.Channels;
using System.ServiceModel.Description;
using System.Text;
using LogService.DataAccessService;
using NLog;
using NLog.LogReceiverService;
using SsoaFramework.Service;
using LogLevel = LogService.DataAccessService.LogLevel;
using System.ServiceModel;

namespace LogService
{
    class Program
    {
        private static ServiceHost silverlightPolicyService;
        public static ManagmentEndpoint ManagmentEndpoint;
        static void Main(string[] args)
        {
            Uri managmentAddress = new Uri("http://localhost:9953/");
            Uri logAddress = new Uri("http://localhost:9900/");
            HostPolicyProvider(managmentAddress);
            HostManagmentEndpoint(new Uri(managmentAddress+"Endpoint/"));
            HostLogService(logAddress);
            Console.ReadKey();
        }

        private static void HostLogService(Uri address)
        {
            StandardizedServiceHost serviceHost = new StandardizedServiceHost(typeof (LogReceiverService), address);
            serviceHost.AddServiceEndpoint(typeof(ILogReceiverServer), new BasicHttpBinding(), String.Empty);
            serviceHost.Open();

            Console.WriteLine("LogService started!");
        }

        static void HostPolicyProvider(Uri address)
        {
            silverlightPolicyService = new ServiceHost(typeof(SilverlightPolicyProvider), address);
            ServiceEndpoint policyEndpoint = silverlightPolicyService.AddServiceEndpoint(typeof(ISilverlightPolicyProvider), new WebHttpBinding(), String.Empty);
            policyEndpoint.Behaviors.Add(new WebHttpBehavior());
            silverlightPolicyService.Open();
            Console.WriteLine("Silverlight policy provider service started! " + address + "ClientAccessPolicy.xml");
        }

        static void HostManagmentEndpoint(Uri address)
        {
            ManagmentEndpoint = new ManagmentEndpoint();
            ServiceHost managmentHost = new ServiceHost(ManagmentEndpoint, address);
            managmentHost.Description.Behaviors.Add(new ServiceMetadataBehavior());
            managmentHost.AddServiceEndpoint(typeof (IManagementEndpoint),
                                        new PollingDuplexHttpBinding(PollingDuplexHttpSecurityMode.None),
                                        address);
            managmentHost.AddServiceEndpoint(typeof (IMetadataExchange),
                                        MetadataExchangeBindings.CreateMexHttpBinding(),
                                        address + "mex");
            
            managmentHost.Open();
            Console.WriteLine("ManagmentEndpoint service started! "+address);
        }
    }
}
